package com.lanlengran.lancher.utils;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.widget.Toast;

import com.lanlengran.lancher.AppDetail;
import com.lanlengran.lancher.MyApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ruiqin on 17-1-16.
 */

public class AppUtils {
    public static List<AppDetail> loadApps() {
        PackageManager pm = MyApplication.getIntance().getApplicationContext().getPackageManager();
        List apps = new ArrayList<AppDetail>();
        Intent i = new Intent(Intent.ACTION_MAIN, null);
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> infos = pm.queryIntentActivities(i, 0);
        for (ResolveInfo info : infos) {
            AppDetail app = new AppDetail();
            app.setLabel(info.loadLabel(pm));
            app.setName(info.activityInfo.packageName);
            app.setIcon(info.loadIcon(pm));
            apps.add(app);
        }
        return apps;
    }

    /*
* 启动一个app
*/
    public static void startAPP(String appPackageName){
        try{
            Intent intent = MyApplication.getIntance().getApplicationContext().getPackageManager().getLaunchIntentForPackage(appPackageName);
            MyApplication.getIntance().getApplicationContext().startActivity(intent);
        }catch(Exception e){
            Toast.makeText(MyApplication.getIntance().getApplicationContext(), "找不到应用程序", Toast.LENGTH_LONG).show();
        }
    }
}
